import logging
import os
from logging.handlers import RotatingFileHandler
from app.config.config import BASE_DIR, config
from app.settings import env

def set_log():
  log_level = config[env].LOG_LEVEL
  logging.basicConfig(level=log_level)
  # 创建日志记录器，指定日志的保存路径、每个日志文件的最大大小、保存的日志文件个数
  file_handler = RotatingFileHandler(os.path.join(BASE_DIR, 'log/app.log'), 
                                     maxBytes=1024*1024, 
                                     backupCount=3,
                                     encoding='utf-8')
  file_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
  logging.getLogger().addHandler(file_handler)


set_log()